define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            function getUrlParameter(name) {
                var search = location.search;
                // 处理可能的多个 ? 分隔符，先把所有 ? 替换为 &
                search = search.replace(/\?/g, '&');
                // 去掉开头的 &
                if (search.startsWith('&')) {
                    search = search.slice(1);
                }
                // 分割参数为键值对数组
                var params = search.split('&');
                for (var i = 0; i < params.length; i++) {
                    var param = params[i].split('=');
                    if (param[0] === name) {
                        return decodeURIComponent(param[1].replace(/\+/g, ' '));
                    }
                }
                return '';
            }

            // 获取 type 参数的值
            var typeValue = getUrlParameter('type');
            if(typeValue==1){
                var show_status=false;
            }else{
                var show_status=true;
            }
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/payment/records/index' + location.search,
                    add_url: 'user/payment/records/add',
                    edit_url: 'user/payment/records/edit',
                    del_url: 'user/payment/records/del',
                    multi_url: 'user/payment/records/multi',
                    import_url: 'user/payment/records/import',
                    table: 'user_payment_records',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                // onLoadSuccess:function (data){
                //     table.bootstrapTable('hideColumn',data.type==1?'water_photo':'')
                //     table.bootstrapTable('collapseAllRows');
                // },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.username', title: __('用户名称'), operate: 'LIKE'},
                        {field: 'homeowner.username', title: __('房东名称'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('已缴费'),"0":__('未缴费')}, formatter: Table.api.formatter.status},
                        {field: 'orders.pay_type', title: __('支付类型'), searchList: {"1":__('线上'),"2":__('线下')}, formatter: Table.api.formatter.status},
                        {field: 'orders.order_sn', title: __('订单号'), operate: 'LIKE'},
                        {field: 'orders.pay_sn', title: __('支付单号'), operate: 'LIKE'},
                        {field: 'jie_status', title: __('结算状态'), searchList: {"1":__('已结算'),"0":__('未结算')}, formatter: Table.api.formatter.normal},
                        {field: 'orders.tui_status', title: __('退款状态'), searchList: {"1":__('已退款'),"0":__('可退款')}, formatter: Table.api.formatter.normal},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'water_photo', title: __('Water_photo'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false,visible:show_status},
                        {field: 'electricity_photo', title: __('Electricity_photo'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false,visible:show_status},
                        {field: 'other_fee', title: __('Other_fee'), operate:'BETWEEN',visible:show_status},
                        {field: 'money_electricity', title: __('Money_electricity'), operate:'BETWEEN',visible:show_status},
                        {field: 'money_water', title: __('Money_water'), operate:'BETWEEN',visible:show_status},
                        {field: 'last_water_num', title: __('Last_water_num'), operate:'BETWEEN',visible:show_status},
                        {field: 'last_electricity_num', title: __('Last_electricity_num'), operate:'BETWEEN',visible:show_status},
                        {field: 'water_num', title: __('Water_num'), operate:'BETWEEN',visible:show_status},
                        {field: 'electricity_num', title: __('Electricity_num'), operate:'BETWEEN',visible:show_status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: '结算',
                                    text: __('结算'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'user/payment/records/jie',
                                    refresh: true
                                },
                                {
                                    name: '退款',
                                    text: __('退款'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'user/payment/records/tui',
                                    refresh: true
                                }
                            ], formatter: Table.api.formatter.operate}                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'user/payment/records/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'user/payment/records/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'user/payment/records/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
